package com.kangfu.api.controller;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.kangfu.api.interceptor.AuthorizationInterceptor;
import com.kangfu.api.service.ITakeMedicineRecordService;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.common.model.AjaxResult;
import com.kangfu.db.model.request.TakeMedicineRequest;
import com.kangfu.db.model.vo.TakeMedicineInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PastOrPresent;
import java.time.LocalDate;
import java.util.List;

@RestController
@Validated
@RequestMapping("/takemedicine")
public class TakeMedicineRecordController {

    @Autowired
    public ITakeMedicineRecordService takeMedicineRecordService;


    @GetMapping("/list")
    public AjaxResult selectTakeMedicineListByDate(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId,  @Valid @NotBlank(message = "请选择日期") @RequestParam(name = "remindDate") String remindDate) {
        LocalDate localDate = LocalDateTimeUtil.parseDate(remindDate);
        if (localDate == null) {
            throw new BusinessException("参数错误");
        }
        List<TakeMedicineInfoVo> list = takeMedicineRecordService.selectTakeMedicineListByDate(userId, localDate);
        return AjaxResult.success(list);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId, @RequestBody TakeMedicineRequest takeMedicineRequest) {
        takeMedicineRecordService.add(userId, takeMedicineRequest);
        return AjaxResult.success();
    }


    @GetMapping("/checkin")
    public AjaxResult checkin(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId, @RequestParam(name = "id", defaultValue = "0") int id) {
        if (0 >= id) {
            throw new BusinessException("参数错误");
        }
        takeMedicineRecordService.checkin(userId, id);
        return AjaxResult.success();
    }

    @GetMapping("/del")
    public AjaxResult del(@RequestAttribute(AuthorizationInterceptor.LOGIN_USER_ID) int userId,@RequestParam(name = "id", defaultValue = "0") int id) {
        if (0 >= id) {
            throw new BusinessException("参数错误");
        }
        takeMedicineRecordService.del(userId, id);
        return AjaxResult.success();
    }
}
